<template>
	<view class="content">
		<u--image :showLoading="true" src="../../static/yuyue_cg.png" width="472rpx" height="422rpx"
			radius="0"></u--image>
		<!-- <u--image :showLoading="true" src="../../static/yuyue_sb.png" width="430rpx" height="392rpx"
			radius="0"></u--image>	 -->
		<text class="result_text">1V2预约</text>
		<text class="result_tips">快去选取会员卡，开始上课吧～</text>
		<button :style="{'background':$store.state.buttonColor}" @click="LinkUrl()">选取会员卡</button>
		<u-popup :show="IsVip" @close="IsVip = false" @open="IsVip = true" mode="center" round="20">
			<view class="addNewpopupBody">
				<text class="title">请先购卡再约课</text>
				<view class="but_flex">
					<button class="rightButton" :style="{'background':$store.state.buttonColor}"
						@click="qxiaoNew">确认</button>
				</view>
			</view>
		</u-popup>
	</view>

</template>

<script>
	import jWeixin from '../../js_sdk/jweixin-module'
	import {
		redirectUrl,
		sharingdata
	} from '../../api/login.js';
	import {
		getShareOrderData,
		userinfo
	} from '../../api/Mine.js';
	import {
		fitness_course_add
	} from '../../api/Teacher.js';
	export default {
		data() {
			return {
				IsVip:false,
				order_id: '',
				shop_id: '',
				Payinfo: {},
				user_vip_id:0,
				addinfo:[]
			}
		},
		onUnload() {
			console.log(3323222)
			uni.$off('changeInvoiceClose');
		},
		onLoad(options) {
			this.order_id = options.order_id || 0;
			this.getWeChatCode();
			uni.$off('changeInvoiceClose');
		},
		onShow(){
			uni.$on('changeInvoiceClose', (data) => {
				this.user_vip_id = data.id
				console.log(data)
				// uni.showLoading({
				// 	title: "加载中"
				// });
				// if (this.req.text_box_id == 0) {
				// 	setTimeout(() => {
				// 		uni.hideLoading()
				// 		uni.navigateTo({
				// 			url: '/pages/Mine/Tutorial_courses?dianji=' + 1 + '&teacher_id=' + this
				// 				.req.teacher_id + '&type=' + this.type
				// 		})
				// 	}, 500)
				// }
				let param = {
					...this.Payinfo,
					...{
						user_vip_id:this.user_vip_id,
						order_id:this.order_id
					}
				}
				console.log(param,'sssssssss')
				this.fitness_course_add(param);
			})
			
		},
		methods: {
			/**
			@method 公众号授权登录
			**/
			async redirectUrl() {
				try {
					const res = await redirectUrl({
						order_id:this.order_id
					});
					if (res?.data) {
						window.location.href = res.data;
					}
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			getWeChatCode() {
				let code = this.getUrlCode('token');
				if (code == null) {
					this.redirectUrl();
				} else {
					this.sendCode(code)
				}
			},
			//方法:用来提取code
			getUrlCode(name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
				[, ''
				])[1].replace(/\+/g, '%20')) || null
			},
			getpower() {
				let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
				let appid = '';
				window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
					'&redirect_uri=' + local + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
			},
			sendCode(code) {
				uni.setStorageSync('token', code);
				if(this.getUrlCode('shop_id')){
					this.shop_id = this.getUrlCode('shop_id');
					uni.setStorageSync('shop_id', this.getUrlCode('shop_id'));
				}
				this.getShareOrderData(this.order_id);
				this.userinfo();
				console.log('打印codesssssssssssss');
				console.log(code);
			},
			/**
			@method 获取order_id
			**/
			async getShareOrderData(order_id = null) {
				try {
					const res = await getShareOrderData({
						order_id: order_id
					});
					this.Payinfo = {
						...res.data,
						...{
							shop_id: this.shop_id
						}
					};
					//this.fitness_course_add(res.data);
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			/**
			@method 调起微信支付
			**/
			async fitness_course_add(Payinfo = {}) {
				try {
					const res = await fitness_course_add(Payinfo);
					// this.Payinfo = {
					// 	...this.Payinfo,
					// 	...res.data,
					// 	...{
					// 		money: res.data.pay_price
					// 	}
					// };
					uni.showToast({
						title: res.code == 1 ? '预约成功' : '预约失败',
						icon: 'none',
						duration: 2000,
						   success: function () {
						        // 延迟2秒后跳转页面
						        setTimeout(function () {
									uni.reLaunch({
										// url: '/pages/Login/LoginHome',
										url: '/pages/Tabbar/Home/Home',
									})
								},2000)
						    }
					});
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			async userinfo() {
				try {
					const res = await userinfo({
						shop_id: uni.getStorageSync('shop_id') || 1
					});
					this.addinfo = res.data.userinfo
					if(this.addinfo['1v2_num'] <= 0){
						this.IsVip = true
					}
				} catch (err) {
					//TODO handle the exception
					console.log(err);
				}
			},
			wxH5pay(res) {
				var _this = this;
				jWeixin.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,线上环境需要改为false
					appId: res.appId, // 必填，公众号的唯一标识
					timestamp: res.timestamp, // 必填，生成签名的时间戳
					nonceStr: res.nonceStr, // 必填，生成签名的随机串
					signature: res.paySign, // 必填，签名
					jsApiList: ['chooseWXPay'], // 必填，需要使用的JS接口列表
				});
				jWeixin.ready(() => {
					jWeixin.chooseWXPay({
						appId: res.appId, // 必填，公众号的唯一标识
						timestamp: res.timestamp,
						nonceStr: res.nonceStr, // 支付签名随机串，不长于 32 位
						package: res.package, // 统一支付接口返回的prepay_id参数值
						signType: res.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
						paySign: res.paySign, // 支付签名
						success: function(res) { //支付成功回调
							uni.showToast({
								title: '支付成功',
								icon: 'none',
								duration: 2000,
							});
							uni.switchTab({
								url: '/pages/Tabbar/Home/Home'
							});
						},
						cancel: function(res) {
							uni.showToast({
								title: '取消支付',
								icon: 'none',
								duration: 2000,
							})
							alert('取消支付')
						},
						fail: function(res) {
							console.log("fail", res);
							uni.showToast({
								title: '支付失败',
								icon: 'none',
								duration: 2000,
							});
							uni.switchTab({
								url: '/pages/Tabbar/Home/Home'
							});
						}
					});
				});
			},
			// linkUrl(e){
			// 	uni.switchTab({
			// 		url: e
			// 	});
			// },
			LinkUrl(e) {
					uni.navigateTo({
						url: '/pages/Mine/MyVip'
					});
			},
			qxiaoNew() {
				this.IsVip = false
				uni.switchTab({
					url: '/pages/Tabbar/Home/Home'
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #f7f7f7;
	}
</style>
<style scoped lang="scss">
	button::after{ border: none;} 
	.addNewpopupBody {
		width: 550rpx;
		height: 250rpx;
		padding-top: 50rpx;
		background-color: transparent;
	
		.title {
			font-family: PingFangSC, PingFang SC;
			font-weight: 600;
			font-size: 36rpx;
			color: #333333;
			display: block;
			text-align: center;
		}
	
		.but_flex {
			display: flex;
			justify-content: space-between;
			align-items: center;
			//margin-top: 0rpx;
			height: 100rpx;
			.leftButton {
				width: 40%;
				height: 74rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 32rpx;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50rpx;
				background-color: #efefef;
			}
	
			.rightButton {
				width: 40%;
				height: 74rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 32rpx;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				border-radius: 50rpx;
				background-color: #267fff;
			}
		}
	}
	.content {
		/deep/ .u-image {
			display: block;
			margin: 0px auto;
			margin-top:100rpx;
		}

		.result_text {
			display: block;
			text-align: center;
			margin: 0px auto;
			font-family: PingFangSC, PingFang SC;
			font-weight: 600;
			font-size: 36rpx;
			color: #333333;
			margin-top:20rpx;
		}

		.result_tips {
			display: block;
			text-align: center;
			margin: 0px auto;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #888888;
			margin-top:10rpx;
			letter-spacing:2rpx;
		}
		
		button{
			width:60%;
			height:95rpx;
			margin:0px;
			// background: linear-gradient( 270deg, #3078FF 0%, #009DFE 100%);
			border-radius:50rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #FFFFFF;
			display: flex;
			justify-content: center;
			align-items: center;
			margin:0px auto;
			margin-top:150rpx;
		}
	}
</style>